package cn.csq.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception{
        //本地文件读取

        File file = new File("/Users/csq/Desktop/study/code/netty-study/src/main/resources/fileChannel.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        //将通道数据读取到buffer
        fileChannel.read(byteBuffer);

        //将buffer的字节数据转为string  byteBuffer.array就是返回buffer内部中用于存储数据的数组
        System.out.println(new String(byteBuffer.array()));


        fileInputStream.close();

    }
}
